package com.tiger.cloud.service.crm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tiger.cloud.common.exception.ServiceException;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.dao.crm.CommucationDao;
import com.tiger.cloud.dao.crm.PeersDao;
import com.tiger.cloud.dao.crm.RecommandListDao;
import com.tiger.cloud.enums.CommucationCustomerType;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.model.crm.Commucation;
import com.tiger.cloud.model.crm.Peers;
import com.tiger.cloud.model.crm.RecommandList;
import com.tiger.cloud.util.ErrorCode;

/**
 * 同行信息
 */
@Service("peersService")
public class PeersService extends TigerBaseService<Peers> implements IPeersService{
    
    @Autowired
	private PeersDao peersDao;
    @Autowired
	private RecommandListDao recommandListDao;
    @Autowired
   	private CommucationDao commucationDao;

	@Override
	public MybatisTigerDao<Peers> getEntityDao() {
		return peersDao;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean deletePeers(Peers peers,Long userId) {
		RecommandList recommandList=new RecommandList();
		recommandList.setUserId(peers.getUserId());
		recommandList.setRecommandId(peers.getPeerId());
		RecommandList findRecommandList=recommandListDao.getRecommand(recommandList);
		if(findRecommandList!=null){
			//之前已经有一方是被删除过的
			if(findRecommandList.getIsDelete()==DeleteEnum.DELETE||findRecommandList.getAcceptIsDelete()==DeleteEnum.DELETE){
				//直接删除
				recommandListDao.removeByUserId(new RecommandList(){
					private static final long serialVersionUID = 1L;
				{
					setUserId(peers.getUserId());
					setRecommandId(peers.getPeerId());
				}});
				if(peersDao.updateByUserId(new Peers(){
					private static final long serialVersionUID = 1L;
					{
						setUserId(peers.getUserId());
						setPeerId(peers.getPeerId());
						setIsDelete(DeleteEnum.DELETE);
					}})>0){
					Commucation commucation=new Commucation();
					commucation.setUserId(peers.getUserId());
					commucation.setCustomerId(peers.getPeerId());
					commucation.setCustomerType(CommucationCustomerType.PEERS);
					commucation.setIsDelete(DeleteEnum.DELETE);
					commucationDao.updateByUserId(commucation);
					return true;
				}
			}else{
				//之前没有被删除过
				//添加方删除
				if(userId.longValue()==findRecommandList.getUserId().longValue()){
					recommandListDao.fixedUpdateByUserId(new RecommandList(){
						private static final long serialVersionUID = 1L;
					{
						setUserId(peers.getUserId());
						setRecommandId(peers.getPeerId());
						setIsDelete(DeleteEnum.DELETE);
					}});
				}else{//接收方删除
					recommandListDao.fixedUpdateByUserId(new RecommandList(){
						private static final long serialVersionUID = 1L;
					{
						setUserId(peers.getPeerId());
						setRecommandId(peers.getUserId());
						setAcceptIsDelete(DeleteEnum.DELETE);
					}});
				}
				if(peersDao.update(peers)>0){
					Commucation commucation=new Commucation();
					commucation.setUserId(peers.getUserId());
					commucation.setCustomerId(peers.getPeerId());
					commucation.setIsDelete(DeleteEnum.DELETE);
					commucation.setCustomerType(CommucationCustomerType.PEERS);
					commucationDao.fixedUpdateByUserId(commucation);
					return true;
				}
			}
		}else{
			throw new ServiceException(ErrorCode.NOT_FIND_REIENDS);
		}
		throw new ServiceException(ErrorCode.REMOVE_DATE_ERROR);
	}

	@Override
	public boolean updateByUserId(Peers peers) {
		if(peersDao.updateByUserId(peers)>0){
			return true;
		}
		throw new ServiceException(ErrorCode.UPDATE_PEERS_ERROR);
	}


}